<?php

use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;

class Messages extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function up()
    {
        $this->table('private_messages')
            ->addColumn('message_type', 'string', ['limit' => '50', 'comment' => '私信类型，使用对应类名表示类型'])
            ->addColumn('to_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '接收者用户ID'])
            ->addColumn('from_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '发送者用户ID'])
            ->addColumn('content', 'text', ['comment' => '私信内容'])
            ->addColumn('payload', 'string', ['limit' => 1000, 'comment' => '功能性负荷，与私信类型配合'])
            ->addColumn('is_read', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'comment' => '是否已经读过'])
            ->addColumn('create_at', 'datetime')
            ->addColumn('update_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
            ->addIndex(['message_type'])
            ->addIndex(['to_id'])
            ->addIndex(['from_id'])
            ->addIndex(['is_read'])
            ->save();

        $this->table('comment_messages')
            ->addColumn('question_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '题目ID'])
            ->addColumn('commenter_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '留言用户ID'])
            ->addColumn('request_check', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'comment' => '是否请求重审'])
            ->addColumn('is_rechecked', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'comment' => '是否已经重审过了'])
            ->addColumn('content', 'text', ['comment' => '留言'])
            ->addColumn('create_at', 'datetime')
            ->addColumn('update_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
            ->addIndex(['question_id'])
            ->addIndex(['commenter_id'])
            ->addIndex(['request_check', 'is_rechecked'])
            ->save();

        $this->table('board_messages')
            ->addColumn('author_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR, 'comment' => '留言者用户ID'])
            ->addColumn('content', 'text', ['comment' => '留言内容'])
            ->addColumn('create_at', 'datetime')
            ->addColumn('update_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
            ->addIndex(['author_id'])
            ->save();

        $this->table('notice_messages')
            ->addColumn('content', 'text', ['comment' => '留言内容'])
            ->addColumn('create_at', 'datetime')
            ->addColumn('update_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
            ->save();
    }

    public function down()
    {
        $this->table('check_message')->drop();
        $this->table('comment_message')->drop();
        $this->table('board_messages')->drop();
        $this->table('notice_message')->drops();
    }
}
